package com.jfinal.plugin.activerecord.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 
 Field  Type    Null    Key Default Extra
USER_ID int(11) NO  PRI \N  auto_increment
REAL_NAME   varchar(100)    YES     \N  
PASSWORD    varchar(50) NO      \N  
NICK_NAME   varchar(100)    YES     \N  
LAST_time   datetime    NO      \N  
ACCOUNT varchar(50) NO      \N  
 
 
 *
 */

@SuppressWarnings("serial")
public class UsersMode extends Model<UsersMode> {
	
	public static final String tableName = "users" ;
	
	public static final UsersMode me = new UsersMode();
	
	public UsersMode()
	{
	    this.getAttrs().put( "lastTime", new Date() );
	}
	
	public UsersMode(  Map<String, Object> attrs )
	{
	    this.getAttrs().putAll( attrs );
	}
	
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<UsersMode> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from "+tableName+" order by userId asc");
	}
	
	public Page<UsersMode> paginate(int pageNumber, int pageSize, Map<String,Object> params ) {
	    
	    List<Object> objs = new ArrayList<Object>() ;
	    String sqlExceptSelect = " from "+tableName+" where " ;
	    Iterator<Map.Entry<String, Object>> iter = params.entrySet().iterator() ;
	    while( iter.hasNext())
	    {
	        Entry<String, Object> entry = iter.next() ;
	        sqlExceptSelect += (entry.getKey() ) ;
	        if ( entry.getValue() != null )
	            objs.add( entry.getValue() ) ; 
	    }
	    sqlExceptSelect += " order by userId asc " ;
	    
	    return paginate(pageNumber, pageSize, "select *",  sqlExceptSelect, objs.toArray()) ;
	}
  
}
